<template>
  <div class="distribution-commissions">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>佣金记录</span>
        </div>
      </template>

      <!-- 搜索条件 -->
      <el-form :inline="true" class="search-form">
        <el-form-item label="分销员姓名">
          <el-input
            v-model="searchForm.customer_name"
            placeholder="请输入分销员姓名"
            clearable
            @clear="handleSearch"
          />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="searchForm.status" placeholder="请选择状态" clearable>
            <el-option label="全部" value="" />
            <el-option label="待结算" value="0" />
            <el-option label="已结算" value="1" />
            <el-option label="已提现" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 佣金记录列表 -->
      <el-table
        :data="tableData"
        border
        stripe
        v-loading="loading"
      >
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column label="分销员" min-width="150">
          <template #default="scope">
            <div>{{ scope.row.customer_name }}</div>
            <div style="font-size: 12px; color: #999;">{{ scope.row.telephone }}</div>
          </template>
        </el-table-column>
        <el-table-column label="来源用户" min-width="150">
          <template #default="scope">
            <div>{{ scope.row.from_customer_name || '直接购买' }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="order_no" label="订单号" width="180" />
        <el-table-column label="订单金额" width="120">
          <template #default="scope">
            ¥{{ scope.row.order_amount }}
          </template>
        </el-table-column>
        <el-table-column label="佣金金额" width="120">
          <template #default="scope">
            ¥{{ scope.row.commission_amount }}
          </template>
        </el-table-column>
        <el-table-column prop="level" label="分销层级" width="100">
          <template #default="scope">
            <el-tag v-if="scope.row.level === 1">一级</el-tag>
            <el-tag v-else type="success">二级</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="状态" width="100">
          <template #default="scope">
            <el-tag v-if="scope.row.status === 0">待结算</el-tag>
            <el-tag v-else-if="scope.row.status === 1" type="success">已结算</el-tag>
            <el-tag v-else type="warning">已提现</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="创建时间" width="180" />
        <el-table-column label="结算时间" width="180">
          <template #default="scope">
            {{ scope.row.settle_time || '-' }}
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @current-change="handlePageChange"
        :current-page="currentPage"
        :page-size="pageSize"
        layout="total, prev, pager, next"
        :total="total"
        style="margin-top: 20px; text-align: right;"
      />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import axios from 'axios'
import { ElMessage } from 'element-plus'

const VITE_URL = (window as any).VITE_URL

// 表格数据
const tableData = ref([])
const loading = ref(false)
const currentPage = ref(1)
const pageSize = ref(15)
const total = ref(0)

// 搜索表单
const searchForm = ref({
  customer_name: '',
  status: ''
})

// 获取佣金记录列表
const getCommissionList = async () => {
  loading.value = true
  try {
    const params = new URLSearchParams()
    params.append('page', currentPage.value.toString())
    params.append('limit', pageSize.value.toString())
    
    if (searchForm.value.customer_name) {
      params.append('customer_name', searchForm.value.customer_name)
    }
    if (searchForm.value.status !== '') {
      params.append('status', searchForm.value.status)
    }
    
    const url = `${VITE_URL}/admin.distribution/commission_list?${params.toString()}`
    const res = await axios.get(url)
    if (res.data.status === 0) {
      tableData.value = res.data.data.list
      total.value = res.data.data.total
    }
  } catch (error) {
    ElMessage.error('获取佣金记录列表失败')
  } finally {
    loading.value = false
  }
}

// 搜索
const handleSearch = () => {
  currentPage.value = 1
  getCommissionList()
}

// 重置
const handleReset = () => {
  searchForm.value = {
    customer_name: '',
    status: ''
  }
  currentPage.value = 1
  getCommissionList()
}

// 分页变化
const handlePageChange = (page: number) => {
  currentPage.value = page
  getCommissionList()
}

onMounted(() => {
  getCommissionList()
})
</script>

<style scoped>
.distribution-commissions {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-form {
  margin-bottom: 20px;
}
</style>